import os.path
import shutil
import re
import os




class GetYoloTxt(object):
    def __init__(self,name,pathname,filename,transmission_num):
        self.name = name
        # self.filename = filename
        self.pathname=pathname
        self.transmission_num=transmission_num


    def gettxt():
        return 333

    def getFile(self):    

        dict_name={
            "2": "0",
            "1": "1",
            "3": "2",
            "year2": "3"
            }

        transmission_num=self.transmission_num
        # pathname='1.txt'
        # pathnames=self.pathname  
        # pathname='1.txt'
        pathnames=''


        filepath=self.pathname
        pathDir = os.listdir(filepath)
        for allDir in pathDir:
            flag=1
            childpath = os.path.join(filepath, allDir)
            if os.path.isfile(childpath):
                print('childpath',childpath)
                # getFile2(child,allDir,transmission,file_name_back)
                with open(childpath, 'r') as f:
                    # print(f.read())
                    data=f.readlines()
                    # print(data)
                    for i in data:
                        a = i.split()
                        print('a------------',a)
                        first_data=a[0] #头部
                        if first_data !=transmission_num:
                            flag=0
                            print('flag',flag)
                if flag==1:
                    pathnames=childpath
                    # pathnames='E:\\dataset_perfect\\all_need_test\\car\\labels\\test_midcar_199.txt'

                    f=open(pathnames,'r')
                    data=f.readlines()
                    for i in data:
                
                        a = i.split()
                        c = a[0]   # 读取该行的第一个数据

                        # if c=='3' :                 # 修改---------------------------------------
                        if c==transmission_num :    
                            # save = "/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all/diningtable/labels"     # 修改---------------------------------------
                            # E:\dataset_perfect\all_need_test\all_data_new_change\car
                          
                            name=self.name
                            # save = "E:\\dataset_perfect\\all_need_test\\treatment2\\{}\\labels".format(name) 
                            save = "E:\\dataset_perfect\\all_need_test\\all_data_new_change\\{}\\labels".format(name) 
                            if not os.path.exists(save):
                                os.makedirs(save)
                            # save_path = os.path.join(save, pathname)
                            save_path = os.path.join(save, allDir)
                            print('save_path',save_path)

                            first_data=dict_name[transmission_num]

                            file = open(save_path, 'a')

                            file.write(first_data+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')   

                            # file.write(transmission_num+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')    # 修改-------------
                            print(i)
                            file.close()             
                    f.close()    
    
 

        # pathnames='E:\\dataset_perfect\\all_need_test\\car\\labels\\test_midcar_199.txt'

        # f=open(pathnames,'r')
        # data=f.readlines()
        # for i in data:
    
        #     a = i.split()
        #     c = a[0]   # 读取该行的第一个数据

        #     # if c=='3' :                 # 修改---------------------------------------
        #     if c==transmission_num :    
        #         # save = "/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all/diningtable/labels"     # 修改---------------------------------------
        #         name=self.name
        #         save = "E:\\dataset_perfect\\all_need_test\\treatment2\\{}\\labels".format(name) 
        #         if not os.path.exists(save):
        #             os.makedirs(save)
        #         save_path = os.path.join(save, pathname)

        #         file = open(save_path, 'a')

        #         file.write(transmission_num+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')    # 修改-------------
        #         print(i)
        #         file.close()             
        # f.close()    



if __name__ == "__main__":
    
    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/voc_5c/labels'
    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/voc_5c/labels'
    
    # arr = []
    # file_name_back='diningtable'
    transmission_num='3'
    # eachFile(filenames,transmission_num,file_name_back)
    # pathname='E:\\dataset_perfect\\all_need_test\\test\\labels'
    # pathname='E:\\dataset_perfect\\all_need_test\\test\\labels2'

    # pathname='E:\\dataset_perfect\\all_need_test\\car\\labels'
    # pathname='E:\\dataset_perfect\\all_need_test\\bike\\labels'
    # pathname='E:\\dataset_perfect\\all_need_test\\motorbike\\labels'
    pathname='E:\\dataset_perfect\\all_need_test\\motorbike\\labels'


    filename=''
    name='motorbike'
    filename=''


    getfile=GetYoloTxt(name,pathname,filename,transmission_num)
    getfile.getFile()





